package com.cbs.android.component.weather.caiyun;


import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;

import com.cbs.android.component.log.L;
import com.cbs.android.component.network.HttpClient;
import com.cbs.android.component.network.Request;
import com.cbs.android.component.network.decoder.GsonDecoder;
import com.cbs.android.component.weather.caiyun.entity.CaiyunWeather;
import com.cbs.android.component.weather.weatherinterface.WeatherHandler;
import com.cbs.android.component.weather.weatherinterface.WeatherInterface;

/**
 * Created by broche on 6/8/16.
 */
public class Caiyun implements WeatherInterface {
    private static final String TAG = Caiyun.class.getName();
    private String key = "";
    private String urlTpl = "https://api.caiyunapp.com/v2/%s/%s/forecast.json";
    private HttpClient httpClient = new HttpClient.Builder().build();

    public Caiyun(Context context) {
        try {
            ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
            key = applicationInfo.metaData.getString("CAIYUN_KEY");
        } catch (PackageManager.NameNotFoundException e) {
            L.e(TAG, "", e);
        }
    }

    @Override
    public void getForecast(final double lat, final double lng, final WeatherHandler weatherListener) {
        Request request = new Request(String.format(urlTpl, key, lng + "," + lat))
                .setDecoder(new GsonDecoder(CaiyunWeather.class))
                .setResponseHandler(new ForecastResponseHandler(lat, lng, weatherListener));
        httpClient.send(request);
    }


    @Override
    public void getForecast(String cityCode, WeatherHandler weatherListener) {
        L.e(TAG, "not implemented");
//        Request request = new Request(String.format(urlTpl, key, cityCode))
//                .setDecoder(new GsonDecoder(CaiyunWeather.class))
//                .setResponseHandler(new ForecastResponseHandler(cityCode, 0, weatherListener));
//        httpClient.send(request);
    }

    @Override
    public void getForecastByCityName(String cityName, WeatherHandler weatherListener) {
        L.e(TAG, "not implemented");
//        Request request = new Request(String.format(urlTpl, key, cityName))
//                .setDecoder(new GsonDecoder(CaiyunWeather.class))
//                .setResponseHandler(new ForecastResponseHandler(cityName, 1, weatherListener));
//        httpClient.send(request);
    }
}
